﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LogLib;
using MetroFramework;
using System.IO;

namespace ScheduleAutoAction
{
    public partial class FormScheduleSettings : Form
    {
        private ScheduleCleaningOption _option = new ScheduleCleaningOption();
        public ScheduleCleaningOption Option
        {
            get { return _option; }
        }

        private ScheduleShutdownOption _shutdownOption = new ScheduleShutdownOption();
        public ScheduleShutdownOption ShutdownOption
        {
            get { return _shutdownOption; }
        }

        public FormScheduleSettings()
        {
            LoadConfig();
            InitializeComponent();
        }

        private void LoadConfig()
        {
            if (!File.Exists(_option.ConfigFilePath))
            {
                _option.Save();
            }
            if (!File.Exists(_shutdownOption.ConfigFilePath))
            {
                _shutdownOption.Save();
            }
            _option.Load();
            _shutdownOption.Load();
        }

        private void OnIntervalChanged()
        {
            switch (metroComboBoxInterval.SelectedIndex)
            {
                case 0: // 每天
                    labelNamed.Visible = true;
                    metroComboBoxWeekDay.Visible = false;
                    dateTimePickerExecuteTime.Visible = true;
                    labelNamed.Text = "的";
                    dateTimePickerExecuteTime.Left = labelNamed.Right + 5;
                    break;
                case 1: // 每周
                    labelNamed.Visible = true;
                    metroComboBoxWeekDay.Visible = true;
                    dateTimePickerExecuteTime.Visible = true;
                    labelNamed.Text = "的";
                    metroComboBoxWeekDay.Left = labelNamed.Right + 5;
                    dateTimePickerExecuteTime.Left = metroComboBoxWeekDay.Right + 5;
                    if (metroComboBoxWeekDay.SelectedIndex < 0)
                    {
                        metroComboBoxWeekDay.SelectedIndex = 2;
                    }
                    break;
                case 2: // 每个月
                    labelNamed.Visible = true;
                    metroComboBoxWeekDay.Visible = true;
                    dateTimePickerExecuteTime.Visible = true;
                    labelNamed.Text = "的第一个";
                    metroComboBoxWeekDay.Left = labelNamed.Right + 5;
                    dateTimePickerExecuteTime.Left = metroComboBoxWeekDay.Right + 5;
                    if (metroComboBoxWeekDay.SelectedIndex < 0)
                    {
                        metroComboBoxWeekDay.SelectedIndex = 2;
                    }
                    break;
                case 3: // 启动时                  
                case 4: // 关闭时
                    labelNamed.Visible = false;
                    metroComboBoxWeekDay.Visible = false;
                    dateTimePickerExecuteTime.Visible = false;
                    break;
                default:
                    break;
            }
        }

        private void InitCtrls()
        {
            metroToggleEnabled.Checked = _option.Enabled;
            metroComboBoxDaysFromToday.SelectedIndex = (int)_option.HistoryFilesFormToday;
            metroToggleWhenOverCV.Checked = _option.OnlyExecuteWhenOverCV;
            intRangeTrackMaxMemorySize.Value = _option.CriticalMemorySize;
            metroToggleInSilence.Checked = _option.ExecuteWithSilence;
            metroToggleEnquiring.Checked = _option.Enquiring;
            metroComboBoxInterval.SelectedIndex = (int)_option.IntervalTime;
            metroComboBoxWeekDay.SelectedIndex = (int)_option.ExecuteWeekDay;
            dateTimePickerExecuteTime.Value = _option.ExecuteTime;
            intRangeTrackMaxMemorySize.Visible = metroToggleWhenOverCV.Checked;
            label6.Visible = metroToggleWhenOverCV.Checked;

            metroToggleEnableAutoShutdown.Checked = _shutdownOption.Enabled;
            dateTimePickerAM.Value = _shutdownOption.ExecuteTimeAtAM;
            dateTimePickerPM.Value = _shutdownOption.ExecuteTimeAtPM;

            OnIntervalChanged();
            OnOptEnabledChanged();
        }

        private void UpdateData()
        {
            _option.Enabled = metroToggleEnabled.Checked;
            _option.HistoryFilesFormToday = (DaysFormToday)metroComboBoxDaysFromToday.SelectedIndex;
            _option.OnlyExecuteWhenOverCV = metroToggleWhenOverCV.Checked;
            _option.CriticalMemorySize = intRangeTrackMaxMemorySize.Value;
            _option.ExecuteWithSilence = metroToggleInSilence.Checked;
            _option.Enquiring = metroToggleEnquiring.Checked;
            _option.IntervalTime = (ExecutionInterval)metroComboBoxInterval.SelectedIndex;
            _option.ExecuteWeekDay = (ExecutionWeekDay)metroComboBoxWeekDay.SelectedIndex;
            _option.ExecuteTime = dateTimePickerExecuteTime.Value;
            _option.Save();

            _shutdownOption.Enabled = metroToggleEnableAutoShutdown.Checked;
            _shutdownOption.ExecuteTimeAtAM = dateTimePickerAM.Value;
            _shutdownOption.ExecuteTimeAtPM = dateTimePickerPM.Value;
            _shutdownOption.Save();
        }

        private void OnOptEnabledChanged()
        {
            bool val = metroToggleEnabled.Checked;
            label1.Enabled = val;
            label2.Enabled = val;
            label3.Enabled = val;
            label4.Enabled = val;
            label5.Enabled = val;
            label6.Enabled = val;
            labelNamed.Enabled = val;
            metroComboBoxDaysFromToday.Enabled = val;
            metroToggleWhenOverCV.Enabled = val;
            intRangeTrackMaxMemorySize.Enabled = val;
            metroToggleInSilence.Enabled = val;
            metroToggleEnquiring.Enabled = val;
            metroComboBoxInterval.Enabled = val;
            metroComboBoxWeekDay.Enabled = val;
            dateTimePickerExecuteTime.Enabled = val;
        }

        private void metroToggleEnabled_CheckedChanged(object sender, EventArgs e)
        {
            OnOptEnabledChanged();
        }

        private void metroToggleWhenOverCV_CheckedChanged(object sender, EventArgs e)
        {
            intRangeTrackMaxMemorySize.Visible = metroToggleWhenOverCV.Checked;
            label6.Visible = metroToggleWhenOverCV.Checked;
        }

        private void FormScheduleSettings_Load(object sender, EventArgs e)
        {
            InitCtrls();
        }

        private void metroComboBoxInterval_SelectedIndexChanged(object sender, EventArgs e)
        {
            OnIntervalChanged();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            UpdateData();
            if (_option.Save())
            {
                Log.WriteAsync("自动清理任务计划选项已保存");
                Log.WriteAsync(_option.ToString());
                AutoMetroMessageBox.Show(this, "自动清理任务计划已经保存..", "保存提示",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information,
                    250, 5);
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            else
            {
                AutoMetroMessageBox.Show(this, "自动清理任务计划保存失败，请稍后重试。.", "保存提示",
                   MessageBoxButtons.OK,
                   MessageBoxIcon.Error,
                   250, 5);
            }
        }

        private void buttonCancell_Click(object sender, EventArgs e)
        {
            Log.WriteAsync("取消了保存自动清理任务计划选项");
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
    }
}
